#include"Options.h"
#include "allocator.h"

CMsgAllocator* theAllocator=0;
CMsgAllocator::CMsgAllocator()
{
	theAllocator = this;
}

CMsgAllocator::~CMsgAllocator()
{
	free_block(ptr_block);
	ptr_block = 0;
	theAllocator = 0;
}

void CMsgAllocator::Create()
{
	CMemory::Create();
	ptr_block = alloc_block();
	alloc_pos  = 0;
}

void CMsgAllocator::adjust(unsigned int require_size)
{
	if((theOptions->max_message_size-alloc_pos)<require_size)
	{
		free_block(ptr_block);
		ptr_block = alloc_block();
		alloc_pos = 0;
	}
}

struct msg*	CMsgAllocator::alloc_sized_msg(unsigned int size)
{
	msg* ret = 0;

	adjust(size);
	ret = CMemory::alloc_msg();
	(ptr_block->duplicate(),ret->data = ptr_block);
	ret->readpos = alloc_pos;
	ret->writepos = alloc_pos;
	ret->totalsize = size;
	alloc_pos += size;

	return ret;
}
